package com.ruoyi.web.controller.nongjt;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.nongjt.domain.SupervisionUnit;
import com.ruoyi.nongjt.service.ISupervisionUnitService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 监管单位管理Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/nongjt/supervisionUnit")
public class SupervisionUnitController extends BaseController
{
    @Autowired
    private ISupervisionUnitService supervisionUnitService;

    /**
     * 查询监管单位管理列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:supervisionUnit:list')")
    @GetMapping("/list")
    public TableDataInfo list(SupervisionUnit supervisionUnit)
    {
        startPage();
        List<SupervisionUnit> list = supervisionUnitService.selectSupervisionUnitList(supervisionUnit);
        return getDataTable(list);
    }

    /**
     * 导出监管单位管理列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:supervisionUnit:export')")
    @Log(title = "监管单位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SupervisionUnit supervisionUnit)
    {
        List<SupervisionUnit> list = supervisionUnitService.selectSupervisionUnitList(supervisionUnit);
        ExcelUtil<SupervisionUnit> util = new ExcelUtil<SupervisionUnit>(SupervisionUnit.class);
        util.exportExcel(response, list, "监管单位管理数据");
    }

    /**
     * 获取监管单位管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('nongjt:supervisionUnit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(supervisionUnitService.selectSupervisionUnitById(id));
    }

    /**
     * 新增监管单位管理
     */
    @PreAuthorize("@ss.hasPermi('nongjt:supervisionUnit:add')")
    @Log(title = "监管单位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SupervisionUnit supervisionUnit)
    {
        // 检查省市区是否已存在
        if (supervisionUnitService.checkRegionExists(supervisionUnit.getProvince(), 
                supervisionUnit.getCity(), supervisionUnit.getDistrict())) {
            return error("该省市区已存在监管单位，请勿重复添加");
        }
        
        return toAjax(supervisionUnitService.insertSupervisionUnit(supervisionUnit));
    }

    /**
     * 修改监管单位管理
     */
    @PreAuthorize("@ss.hasPermi('nongjt:supervisionUnit:edit')")
    @Log(title = "监管单位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SupervisionUnit supervisionUnit)
    {
        return toAjax(supervisionUnitService.updateSupervisionUnit(supervisionUnit));
    }

    /**
     * 删除监管单位管理
     */
    @PreAuthorize("@ss.hasPermi('nongjt:supervisionUnit:remove')")
    @Log(title = "监管单位管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(supervisionUnitService.deleteSupervisionUnitByIds(ids));
    }

    /**
     * 根据省市区查询监管单位
     */
    @GetMapping("/getByRegion")
    public AjaxResult getByRegion(String province, String city, String district)
    {
        SupervisionUnit supervisionUnit = supervisionUnitService.selectSupervisionUnitByRegion(province, city, district);
        return success(supervisionUnit);
    }

    /**
     * 检查省市区是否已存在监管单位
     */
    @GetMapping("/checkRegion")
    public AjaxResult checkRegion(String province, String city, String district)
    {
        boolean exists = supervisionUnitService.checkRegionExists(province, city, district);
        return success(exists);
    }
}
